package Cashier;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Item{//Leong Vai Cheong

	private String barcode;
	private String description;
	private double price;
	private int InventoryQty;
	private int PurchaseQty;
	private double Subtotal;
	
	
	public Item(){}	
	
	public Item(String description, double price){
		this.description = description;
		this.price = price;
	}
	
	public Item(String description, double price, int InventoryQty){
		this(description, price);
		this.InventoryQty = InventoryQty;
		this.PurchaseQty = 1;
		this.Subtotal = this.price * this.PurchaseQty;
	}
	public Item(String description, double price, int InventoryQty, int PurchaseQty){
		this(description, price, InventoryQty);
		this.PurchaseQty = PurchaseQty;
	}
	
	public Item(String barcode, String description, double price, int InventoryQty){
		this(description, price, InventoryQty);
		this.barcode = barcode;
	}	
			
	static public void addData(String tbName, String[][] items, Connection con) throws SQLException {
		String info = "";
		String sqlInsert = "";
		PreparedStatement ps = null;
		for (int i = 0; i < items.length; i++) {
			for (int j = 0; j < items[i].length; j++) {
				info = info + "'" + items[i][j] + "'" + ",";
			}
			System.out.println(info);
			sqlInsert = "INSERT INTO " + tbName + " VALUES(" + info + ")";
			ps = con.prepareStatement(sqlInsert);
			ps.executeUpdate();
			info = "";
		}
		ps.close();
	}
	
	static public List<Item> getData(ResultSet rs) throws SQLException {
		
		List<Item>items = new ArrayList<Item>();	
		while (rs.next()) {
			items.add(new Item(rs.getString("PRODUCTID"), rs.getString("DESCRIPTION"), 
					             rs.getDouble("SOLD_PRICE"), rs.getInt("QUANTITY")));				
		}
		return items;
	}
	
	public static String geneBarCode(int lenght){   		  
	    String val = ""; 	           
	    Random random = new Random(); 
	    
	    for(int i = 0; i < lenght; i++) {   
	    	// To output letters or numbers
	        String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; 
	               
	        if("char".equalsIgnoreCase(charOrNum)) {  // 0--letters  
	        	// 0--uppercase , 1--lowercase
	            int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; 
	            val += (char) (choice + random.nextInt(26));   
	        }   
	        else if("num".equalsIgnoreCase(charOrNum)){  // 1--numbers
	                 
	            val += String.valueOf(random.nextInt(10));   
	        }   
	    }   
	           
	    return val;   
	}
	
	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getBarcode() {
		return barcode;
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}

	public int getInventoryQty() {
		return InventoryQty;
	}

	public void setInventoryQty(int InventoryQty) {
		this.InventoryQty = InventoryQty;
	}

	public int getPurchaseQty() {
		return PurchaseQty;
	}

	public void setPurchaseQty(int purchaseQty) {
		PurchaseQty = purchaseQty;
	}

	public double getSubtotal() {
		return Subtotal;
	}

	public void setSubtotal(double subtotal) {
		Subtotal = subtotal;
	}


}